package factory.one.controller;

import factory.one.entity.Custom;
import factory.one.pojo.common.Page;
import factory.one.pojo.common.SqlCondition;
import factory.one.service.CustomService;
import factory.one.utils.SecurityUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author sk.z
 * 2023/7/20
 * 客户
 **/
@Controller
@RequestMapping("/custom")
public class CustomController {

    private final CustomService customService;

    public CustomController(CustomService customService) {
        this.customService = customService;
    }

    @GetMapping("/add.html")
    public String addHtml() {
        return "custom/add";
    }

    @ResponseBody
    @PostMapping("/add")
    public String add(@RequestParam String name,
                      @RequestParam(defaultValue = "") String phone,
                      @RequestParam(defaultValue = "") String remark) {
        String user = SecurityContextHolder.getContext().getAuthentication().getName();
        Custom custom = new Custom();
        custom.setName(name);
        custom.setPhone(phone);
        custom.setRemark(remark);
        custom.setCreateUser(user);
        custom.setStatus(1);
        return customService.add(custom).toString();
    }

    @GetMapping("/list.html")
    public String listHtml(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "30") Integer size,
                           Model model) {
        List<SqlCondition> conditions = new ArrayList<>(2);
        conditions.add(new SqlCondition("status = ?", 1, Types.INTEGER));
        if (!SecurityUtil.isAdmin()) {
            conditions.add(new SqlCondition("create_user = ?", SecurityUtil.getUserName(), Types.VARCHAR));
        }

        int count = customService.countByCondition(conditions);
        List<Custom> list = customService.listByCondition(page, size, conditions);
        model.addAttribute("page", new Page<>(list, count, page, size));
        return "custom/list";
    }

    @GetMapping("/edit.html")
    public String editHtml(@RequestParam Long customId, Model model) {
        model.addAttribute("custom", customService.getById(customId));
        return "custom/edit";
    }

    @ResponseBody
    @PostMapping("/edit")
    public Integer edit(@RequestParam Long id,
                        @RequestParam String name,
                        @RequestParam(defaultValue = "") String phone,
                        @RequestParam(defaultValue = "") String remark) {
        Custom custom = new Custom();
        custom.setId(id);
        custom.setName(name);
        custom.setPhone(phone);
        custom.setRemark(remark);
        custom.setStatus(1);
        return customService.update(custom);
    }

    @GetMapping("del")
    public String del(Long customId, Model model) {
        customService.del(customId);
        return listHtml(1, 30, model);
    }

}
